package com.lu.service;

import com.lu.bean.AppException;
import com.lu.bean.BranchBean;
import com.lu.bean.SubmitBean;
import com.lu.bean.TaskBean;
import com.lu.constant.ErrorEnum;
import com.lu.constant.ParamConstant;
import com.lu.constant.TypeEnum;
import com.lu.dao.mapper.WgBranchRecordMapper;
import com.lu.dao.mapper.WgGameRecordMapper;
import com.lu.dao.mapper.WgSubmitRecordMapper;
import com.lu.dao.po.WgBranchRecord;
import com.lu.dao.po.WgGameRecord;
import com.lu.dao.po.WgSubmitRecord;
import com.lu.dao.po.WgSubmitRecordExample;
import com.lu.util.CreateIdUtil;
import com.lu.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author issuser
 * @ClassName GameService
 * @Description
 * @date 2021/07/12 20:09
 */
@Service
public class GameService {

    @Resource
    private WgGameRecordMapper wgGameRecordMapper;

    @Resource
    private WgBranchRecordMapper wgBranchRecordMapper;

    @Resource
    private WgSubmitRecordMapper wgSubmitRecordMapper;

    /**
     * @Description
     * @Author issuser
     * @Date 2021/7/12 20:18
     * @param openIds
     * @return java.lang.String
     */
    public String createNewGame(String openIds) throws AppException {

        if(!StringUtils.hasLength(openIds)) {
            throw new AppException(ErrorEnum.PARAM_EMPTY.code,"参与人不能为空");
        }

        int count = 0;
        String[] openIdArray = openIds.split(",");
        for(String openId:openIdArray) {
            if(StringUtils.hasLength(openId)) {
                count++;
            }
        }
        if(count < ParamConstant.MIN_PLAYER) {
            throw new AppException(ErrorEnum.MIN_PLAYER);
        }

        WgGameRecord record = new WgGameRecord();
        record.setGameId(CreateIdUtil.createId("G"));
        record.setParticipant(openIds);
        record.setCount(count);
        record.setCreateTime(DateUtil.getNowTimeStr());
        wgGameRecordMapper.insert(record);

        return record.getGameId();
    }

    public String createBranch(String gameId, String openId, String riddle) throws AppException {

        WgGameRecord gameRecord = wgGameRecordMapper.selectByGameId(gameId);
        if(gameRecord == null) {
            throw new AppException(ErrorEnum.GAME_NOT_EXIST);
        }
        String[] playerArray = gameRecord.getParticipant().split(",");
        List<String> playerList = Arrays.asList(playerArray);
        if(!playerList.contains(openId)) {
            throw new AppException(ErrorEnum.PLAYER_ILLEGAL);
        }
        //TODO branch再校验是否重复提交了(似乎不必校验)

        String openIds = reOrderOpenId(playerList, openId);
        WgBranchRecord record = new WgBranchRecord();
        record.setGameId(gameId);
        record.setBranchId(CreateIdUtil.createId("B"));
        record.setRiddle(riddle);
        record.setCount(gameRecord.getCount());
        record.setParticipant(openIds);
        record.setCreateTime(DateUtil.getNowTimeStr());
        wgBranchRecordMapper.insert(record);

        WgSubmitRecord submitRecord = new WgSubmitRecord();
        submitRecord.setNextOpenId(openId);
        submitRecord.setGuessWord(riddle);
        submitRecord.setType(TypeEnum.RIDDLE.code);
        submitRecord.setGameId(gameId);
        submitRecord.setBranchId(record.getBranchId());
        submitRecord.setOrderNum(-1);
        submitRecord.setCreateTime(DateUtil.getNowTimeStr());
        wgSubmitRecordMapper.insert(submitRecord);

        return record.getBranchId();
    }

    private String reOrderOpenId(List<String> playerList, String openId) {
        //重新排序openIds
        int index = playerList.indexOf(openId);
        StringBuffer playerBuffer = new StringBuffer().append(openId).append(",");
        for(int i = index+1; i < playerList.size(); i++){
            playerBuffer.append(playerList.get(i)).append(",");
        }
        for(int i = 0; i < index; i++) {
            playerBuffer.append(playerList.get(i)).append(",");
        }
        return playerBuffer.toString();
    }


    public List<TaskBean> queryTask(String gameId, String openId) throws AppException {
        List<TaskBean> result = new LinkedList<>();
        List<WgSubmitRecord> list = wgSubmitRecordMapper.queryTask(gameId, openId);
        for(WgSubmitRecord record:list) {
            TaskBean bean = new TaskBean();
            result.add(bean);
            BeanUtils.copyProperties(record, bean);
        }
        return result;
    }

    public List<BranchBean> queryHistoryList(String openId) throws AppException {
        List<BranchBean> result = new LinkedList<>();
        List<WgBranchRecord> list = wgBranchRecordMapper.selectByOpenId(openId);
        for(WgBranchRecord record:list) {
            BranchBean bean = new BranchBean();
            result.add(bean);
            BeanUtils.copyProperties(record, bean);
        }
        return result;
    }
    public List<SubmitBean> queryHistoryDetail(String branchId, String orderNum) throws AppException {

        List<SubmitBean> result = new LinkedList<>();

        WgSubmitRecordExample example = new WgSubmitRecordExample();
        example.createCriteria().andBranchIdEqualTo(branchId).andOrderNumEqualTo(Integer.valueOf(orderNum));
        example.setOrderByClause("create_time desc");
        List<WgSubmitRecord> list = wgSubmitRecordMapper.selectByExample(example);

        for(WgSubmitRecord record:list) {
            SubmitBean bean = new SubmitBean();
            BeanUtils.copyProperties(record, bean);
            result.add(bean);
        }
        return result;
    }



}
